{% extends "base.html" %}
{% set active_page = "stats" %}
{% set page_title = 'User Stats - Mining and account information for user ' +  username %}
{% block body %}

<script>
$(document).ready(function() {
  {# Initial graph generation #}
  generate_graph($('#chart'), {address: '{{ username }}'});

  $('div.collapse-button').bind( "click", function() {
      $(this).text(function(i,old){
          return old=='[ - ]' ?  '[ + ]' : '[ - ]';
      });
  });
  $('div.collapse-button').bind( "click", function() {
      $(this).text(function(i,old){
          return old=='[ - ]' ?  '[ + ]' : '[ - ]';
      });
  });
});
</script>
{% if g.alerts  %}
  {% for alert in g.alerts %}
  {# We only want to display user level alerts here #}
  {% if alert['notify'] == username %}
  <div data-alert-id={{ alert['key'] }} class="alert alert-dismissable alert-{{ alert['severity'] }}">
    <h4{% if not alert['body'] %} style="margin-bottom:0px;"{% endif %}>
      {{ alert['title']|safe }} -
      <span class="text-muted small" style="color:#fff;">
        {{ alert['date'] }}
      </span>
    </h4>
    <p>{{ alert['body'] | safe }}</p>
  </div>
  {% endif %}
  {% endfor %}
{% endif %}
<div class="container">
  <div class="row page-header">
    <h2 style="word-wrap:break-word;margin-bottom:2px;">{{ _("User Stats") }} - {{ username }} </h2>
    {% for curr, name, merged_addr in merged_addrs %}
      <span style="padding-left:15px;">{{ _("Merge mining %(name)s payout address", name=name) }} : {{ merged_addr }}
        <a style="color:gray;" href="/set_merge/{{ username }}/{{ curr }}"><i class="fa fa-edit" ></i></a>
      </span><br>
    {% endfor %}
    <span style="padding-left:15px;">
      <a href="/settings/{{ username }}">
      {{ _("Edit account settings") }} <i style="color:gray;" class="fa fa-edit" ></i></a>
    </span><br>
    <br>
  </div>
  <div class="row row-header">
    <h2>{{ _("Worker Hashrates") }}
      <div class="btn btn-default btn-sm collapse-button" data-toggle="collapse" data-target="#chart" id="hashrate-collapse">{% if hide_hr %}[+]{% else %}[-]{% endif %}</div></h2>
    <div class="col-lg-12 collapse {% if not hide_hr %}in{% endif %}" id="chart">
      <ul class="nav nav-tabs">
        <li class="tab hour"><a>{{ _("Hour") }}</a></li>
        <li class="tab day"><a>{{ _("Day") }}</a></li>
        <li class="tab month"><a>{{ _("Month") }}</a></li>
      </ul>
    <div style="height:400px;">
      <img src="{{ config['assets_address'] | safe }}/img/712.GIF" style="position:absolute;padding-top:150px;padding-left:400px;"><svg></svg>
    </div>
  </div>
  </div>

  {% if workers %}
  <div class="row page-header">
    <h2>{{ _("Worker Status") }}</h2>
    <br>
    {% for worker in workers %}
    <div class="col-lg-10 col-lg-offset-1 col-md-12 col-sm-12 col-xs-12">
      <div class="panel panel-default" style="background-color:#243749;">
        <div class="panel-heading">
          <h3>
            <i class="fa fa-circle" style="color:{% if worker['online'] %}#5BCE00{% else %}#B12627{% endif %}"
               data-toggle="tooltip" data-placement="right"
               title="{% if worker['online'] %}Online{% else %}Offline{% endif %}"></i>&nbsp;
             {{ worker['name'] }} <small>({{ worker['algo'] }})</small>
            {% for server, connections in worker['servers'].iteritems() %}
              &nbsp;&nbsp;<small style="font-size:8pt;">{{ server.display_text }} ({{ connections }} connections)</small>
            {% endfor %}
            <span class="pull-right">
            <small class="text-right" style="padding-left:10px;font-weight:300;">{{ _("Hashrate") }}:
              {{ worker['last_10_shares'].hashrate() | hashrate }} &nbsp;
              <i class="fa fa-question-circle" data-toggle="tooltip" data-placement="right"
                       title="Average hashrate over the last ten minutes based on accepted shares, NOT PPAgent."></i></small>
            <small class="text-right" style="padding-left:10px;font-weight:300;">{{ _("Efficiency") }}:
              {{ worker['total_shares'].efficiency | round(2) }}% &nbsp;
              <i class="fa fa-question-circle" data-toggle="tooltip" data-placement="right"
                       title="Computed from accepted/total({{ worker['total_shares'].accepted }}/{{ worker['total_shares'].total }}) shares submitted in the last 24 hours"></i></small>
            </span>
          </h3>
          <div class="clearfix"></div>
        </div>
        <div class="panel-body" style="padding:0px;">
          <div class="table-responsive  bs-component" style="position:relative;padding:9px;">
            <div class="col-xs-6 text-center">
              <h4>{{ _("Accepted Shares") }}: {{ '{:,}'.format(worker['total_shares'].accepted) }}&nbsp;
                <i class="fa fa-question-circle" data-toggle="tooltip" data-placement="right"
                     title="Accepted shares over the last 24 hours"></i></h4>
            </div>
            <div class="col-xs-6 text-center">
              <h4>{{ _("Rejected Shares") }}: {{ '{:,}'.format(worker['total_shares'].rejected) }}&nbsp;
                <i class="fa fa-question-circle" data-toggle="tooltip" data-placement="right"
                     title="Rejected shares over the last 24 hours"></i></h4>
            </div>
            <div class="clearfix"></div>
          </div>
          {% if worker['status'] %}
          {% set stat = worker['status'] %}
          <div class="table-responsive bs-component worker-table-wrapper">
            <div class="worker-wrapper">
              <b>{{ stat['type'] }} {{ _("status") }}</b>
            {% if not worker['name'] %}
              {# Show a warning about unnamed workers #}
              <small style="font-size:14px;color:#ff0000;font-weight:400;">
                {{ _("Multiple unnamed workers will do wonky things!") }}
              </small>
            {% endif %}
            {% if worker['status_stale'] == True %}
              {# if stale then show a warning #}
              {#
              <small colspan="3" style="font-size:14px;color:#EEA236;"> <i>{{ _("Updated") }} {{ worker['status_time'] | human_date_utc }}</i></small>
              <div class="btn btn-default btn-sm collapse-button" data-toggle="collapse" data-target="#worker-{{ worker['name'] }}">
                [ + ]
              </div>
              #}
            {% else %}
              {# Not stale - show last updated time #}
              {#
              <small colspan="3" style=" font-size:14px;font-weight:300;">
                <i>{{ _("Updated") }} {{ worker['status_time'] | human_date_utc }}</i>
              </small>
              #}
              <div class="btn btn-default btn-sm collapse-button"
                   data-toggle="collapse" data-target="#worker-{{ worker['name'] }}">
                [ - ]
              </div>
            {% endif %}
            </div>
            {# Worker's miner stats #}
            <div id="worker-{{ worker['name'] }}" class="collapse{% if worker['status_stale'] == False %} in{% endif %}">
              <div class="table-responsive  bs-component" style="position:relative;padding:9px;">
                <div class="col-xs-3 text-center">
                  <h4>WU: {{ worker['wu'] | round(2) }}&nbsp;
                    <i class="fa fa-question-circle" data-toggle="tooltip" data-placement="right"
                         title="Work Utility is calculated from accepted n1 shares/time mining. This results in a number that represents the effective contributions, rather than the raw hashrate."></i></h4>
                </div>
                <div class="col-xs-3 text-center">
                  <h4>{{ worker['total_hashrate'] | hashrate }}&nbsp;
                    <i class="fa fa-question-circle" data-toggle="tooltip" data-placement="right"
                         title="Summation of the reported average hashrates"></i>
                  </h4>
                </div>
                <div class="col-xs-3 text-center">
                  <h4>
                    {% if worker['wue'] < 0.80 %}
                    <i class="fa fa-circle" style="color:red"></i>&nbsp;
                    {% elif (worker['wue'] < 0.87) and (worker['wue'] >= 0.80) %}
                    <i class="fa fa-circle" style="color:yellow"></i>&nbsp;
                    {% elif worker['wue'] >= 0.87 %}
                    <i class="fa fa-circle" style="color:green"></i>&nbsp;
                    {% endif %}
                    WUE: {{ (worker['wue']*100)| round(2)}}%&nbsp;
                    <i class="fa fa-question-circle" data-toggle="tooltip" data-placement="right"
                         title="This value is calculated from the reported WU({{ worker['wu'] }}) / reported average hashrate({{ (worker['total_hashrate']*1000)|int }}). A value of 90%+ is ideal. To increase your WUE make sure your rig is not having hardware errors"></i></h4>
                </div>
                <div class="col-xs-3 text-center">
                  <h4><i class="fa fa-bar-chart-o"></i> <a href="/{{ username }}/{{ worker['name'] }}">{{ _("Detailed view") }}</a></h4>
                </div>
                <div class="clearfix"></div>
              </div>
              <table class="table table-striped table-hover worker-table">
                <thead>
                  <tr>
                    <th>{{ _("GPU") }}</th>
                    <th>{{ _("KHash/sec") }}</th>
                    <th>{{ _("WUE") }}</th>
                    <th>{{ _("Accepted/Rejected(%%)") }}</th>
                    <th>{{ _("HW Errors") }}</th>
                    <th>{{ _("Temperature") }}</th>
                    <th>{{ _("Fan Speed") }}</th>
                    <th>{{ _("Core/Memory Clock") }}</th>
                  </tr>
                </thead>
                <tbody>
                {% for gpu in stat['gpus'] %}
                  <tr class="gpu" data-url="/{{ username }}/{{ worker['name'] }}/details/{{ loop.index0 }}">
                    <th>{{ loop.index0 }}</th>
                    <th>
                      {{ (gpu.get('MHS av', 0)*1000)|round|int }}
                    </th>
                    <th>
                      {% set accepted = gpu.get('Difficulty Accepted', 0) %}
                      {% if accepted != 0 and gpu.get('Device Elapsed', 0) !=0 %}
                        {% set WU = (accepted / gpu['Device Elapsed'])*60 %}
                      {% else %}
                        {% set WU = 0 %}
                      {% endif %}
                      {% if WU == 0 or (gpu.get('MHS av', 0.0001) == 0) %}
                        -/-
                      {% else %}
                        {{ ( (WU / (gpu.get('MHS av', 0.0001)*1000) ) * 100 ) | round(1) }}%
                      {% endif %}
                    </th>
                    <th>
                      {{ '{:,}'.format(accepted|int) }} / {{ '{:,}'.format(gpu.get('Difficulty Rejected', 0)|int) }}
                      {% if accepted != 0 %}
                        &nbsp;({{ (((accepted - gpu.get('Difficulty Rejected', 0)) / accepted)*100)|round(2) }}%)
                      {% else %}
                        (0)
                      {% endif %}
                    </th>
                    <th>{{ gpu.get('Hardware Errors', 0) }}</th>
                    <th {% if gpu.get('temp', 0) > 90 %}class="danger"{% endif %}>{{ gpu.get('temp', 0) }}</th>
                    <th>{{ gpu.get('Fan Percent', 0) }}%</th>
                    <th>{{ gpu.get('GPU Clock', 0) }}/{{ gpu.get('Memory Clock', 0) }}</th>
                  </tr>
                {% endfor %}
                  <tr>
                    <th colspan="8" style="text-align:left;">
                      <small>{{ _("Running PPAgent") }} v{{ stat.get('v', '0.2') }}</small>&nbsp;
                      {% if worker['status_version'] < config['latest_ppagent'] %}
                        {# Notice about out of date ppagent #}
                        <a href="https://github.com/icook/ppagent#upgrade">
                          <small style="color:#FFD600;font-weight:400;">
                            {{ _("Newer version available here") }}
                          </small>
                        </a>&nbsp;&nbsp;
                      {% endif %}
                    </th>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
          {% endif %}
        </div>
      </div>
    </div>
    {% endfor %}
  </div>
  {% endif %}

  <div class="row row-header">
    <div class="col-lg-12">
      {% for payout_currency, data in earning_summary.iteritems() %}
      <div class="bs-example table-responsive">
        <h3>{{ payout_currency.name }} {{ _("Unpaid Credit Summary") }}</h3>
        <table class="table table-striped table-hover">
          <tr>
            <th>{{ _("Currency") }}</th>
            <th>{{ _("Total Pending") }}</th>
            <th>{{ _("Immature") }}</th>
            <th>{{ _("Unconverted") }}</th>
            <th>{{ _("Partially Converted (BTC amount)") }}</th>
            <th>{{ _("Payable") }}</th>
          </tr>
          {% for currency, cdata in data['by_currency'].iteritems() %}
          <tr>
            <td>{{ currency }}</td>
            <td>{{ cdata['total_pending'] | currency }} {{ currency }}</td>
            <td>{{ cdata['immature'] | currency }} {{ currency }}</td>
            <td>{% if cdata['convert'] %}{{ cdata['unconverted'] | currency }} {{ currency }}{% else %}N/A{% endif %}</td>
            <td>{% if cdata['convert'] %}{{ cdata['sold'] | currency }} {{ currency }} ({{ cdata['btc_converted'] | currency }} BTC){% else %}N/A{% endif %}</td>
            <td>{{ cdata['payable'] | currency }} {{ payout_currency }}</td>
          </tr>
          {% endfor %}
          <tr>
            <td colspan="4"></td>
            <td style="border-top:1px solid white;">{{ data['sold_btc_total'] | currency }} BTC</td>
            <td style="border-top:1px solid white;">{{ data['payable_total'] | currency }} {{ payout_currency }}</td>
          </tr>
        </table>
      </div>
      {% endfor %}
    </div>
  </div>

  <div class="row row-header">
    <h2>{{ _("Recent Payouts") }}</h2>
    <br>
    {% include "payout_table.html" %}
    <a href="/{{ username }}/account">{{ _("View all account transactions") }}</a>
  </div>

  <div class="row row-header">
    <h2>{{ _("Recent Unpaid Block Credits") }}</h2>
    <br>
    {% include "credit_table.html" %}
    <a href="/{{ username }}/aggr_account">{{ _("View all recent credits") }}</a>
  </div>
{% endblock %}
